from django.db import models
from django.utils.translation import ugettext_lazy as _
from faq.managers import TopicManager, QuestionManager

class Topic(models.Model):
	title			= models.CharField(_('title'), max_length = 128, help_text = _('What is this topic called?'))
	slug			= models.SlugField(_('slug'), unique = True, help_text = _('The slug is a short, human-readable string used in URLs, generated from the title.'))
	description		= models.TextField(_('description'), blank = True, help_text = _('Optional description of this topic.'))
	
	# manager
	objects = TopicManager()

	class Meta:
		verbose_name = _('topic')
		verbose_name_plural = _('topics')
		ordering = ('title',)

	def __unicode__(self):
		return u'%s' % self.title
	
	@models.permalink
	def get_absolute_url(self):
		return ('faq.views.question_list', None, {'topic': self.slug})

class PendingQuestion(models.Model):
	name			= models.CharField(_('name'), max_length = 128, help_text = _('The name of the person who submitted the pending question.'))
	email			= models.EmailField(_('email'), help_text = _('The email address of the person who submitted the pending question.'))
	question		= models.ForeignKey('faq.Question', help_text = _('The question pending.'))
	activation_code	= models.CharField(_('activation code'), max_length = 48, help_text = _('The activation code is a unique string used to verify the submission of the pending question.'))
	
	class Meta:
		verbose_name = _('pending question')
		verbose_name_plural = _('pending questions')

	def __unicode__(self):
		return _('%s submitted by %s') % (self.question, self.name)
	
	@models.permalink
	def get_absolute_url(self):
		return ('faq.views.pending_question_verify', None, {'code': self.activation_code})

class Question(models.Model):
	question		= models.CharField(_('question'), max_length = 128, help_text = _('The question to be answered.'))
	slug			= models.SlugField(_('slug'), unique = True, help_text = _('The slug is a short, human-readable string used in URLs, generated from the question.'))
	answer			= models.TextField(_('answer'), blank = True, help_text = _('The answer to this quesiton.'))
	topic			= models.ForeignKey(Topic, blank = True, null = True, help_text = _('Optional topic under which this quesiton falls.'))
	published		= models.BooleanField(_('published'), default = True, help_text = _('Is this question publically published?'))
	sort_order		= models.IntegerField(_('sort order'), default = 0, help_text = _('Questions are sorted in lists from highest sort order first to lowest last.'))
	date_created	= models.DateField(_('date created'), auto_now_add = True, help_text = _('Date this question was first created.'))
	date_modified	= models.DateField(_('date modified'), auto_now = True, help_text = _('Date this question was last modified.'))

	# manager
	objects = QuestionManager()

	class Meta:
		verbose_name = _('question')
		verbose_name_plural = _('questions')
		ordering = ('-sort_order', '-date_created')

	def __unicode__(self):
		return u'%s' % self.question
	
	@models.permalink
	def get_absolute_url(self):
		if self.topic:
			return ('faq.views.question_list', None, {'topic': self.topic.slug})
		return ('faq.views.question_list', None, None)
